package me.green.dao;

import me.green.bean.Plan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author green
 */
public class PlanDao {

    /**
     * 添加工作计划
     *
     * @param plan
     * @return
     */
    public boolean save(Plan plan) {
        String sql = "insert into plan(plan_date,detail,add_date,uid,state) values(?,?,?,?,'1')";
        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setTimestamp(1, plan.getPlanDate());
            ps.setString(2, plan.getDetail());
            ps.setTimestamp(3, plan.getAddDate());
            ps.setInt(4, plan.getUid());
            int i = ps.executeUpdate();

            ps.close();
            return i > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDB.release(conn);
        }
        return false;
    }

    /**
     * 根据工作计划的ID查询对应的详细信息
     *
     * @param id
     * @return
     */
    public Plan query(int id) {
        Plan plan = null;
        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            String sql = "select id,plan_date,detail,add_date,state from plan where id=? ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                plan = new Plan();
                plan.setId(rs.getInt("id"));
                plan.setPlanDate(rs.getTimestamp("plan_date"));
                plan.setDetail(rs.getString("detail"));
                plan.setAddDate(rs.getTimestamp("add_date"));
                plan.setState(rs.getString("state"));
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDB.release(conn);
        }
        return plan;
    }

    /**
     * 根据日期查询[工作计划]记录
     *
     * @param uid
     * @param start
     * @param end
     * @return java.util.List
     */
    public List<Plan> getPlanByWeek(int uid, java.sql.Date start, java.sql.Date end) {
        List<Plan> list = new ArrayList<Plan>();

        String sql = "SELECT id,plan_date,detail,add_date,state " +
                " FROM plan " +
                " WHERE state<>'0' AND uid=? and plan_date BETWEEN ? AND ?" +
                " ORDER BY plan_date ASC";
        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, uid);
            ps.setDate(2, start);
            ps.setDate(3, end);

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Plan plan = new Plan();
                plan.setId(rs.getInt("id"));
                plan.setPlanDate(rs.getTimestamp("plan_date"));
                plan.setDetail(rs.getString("detail"));
                plan.setAddDate(rs.getTimestamp("add_date"));
                plan.setState(rs.getString("state"));
                list.add(plan);
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDB.release(conn);
        }
        return list;
    }

    public List<Plan> getAllUserPlans(int uid) {
        LinkedList<Plan> list = new LinkedList<Plan>();

        String sql = "select id,plan_date,detail,add_date,state from plan where state<>'0' and uid=?";
        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, uid);

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Plan plan = new Plan();
                plan.setId(rs.getInt("id"));
                plan.setPlanDate(rs.getTimestamp("plan_date"));
                plan.setDetail(rs.getString("detail"));
                plan.setAddDate(rs.getTimestamp("add_date"));
                plan.setState(rs.getString("state"));
                list.add(plan);
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDB.release(conn);
        }

        return list;
    }

    /**
     * 根据ID修改原有的[完成日志]，返回结果为一布尔值
     *
     * @param plan
     * @return
     */
    @Deprecated
    public boolean modPlanById(Plan plan) {
        String sql = "update plan set plan_date=?,detail=? where id=?";
        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setTimestamp(1, plan.getPlanDate());
            ps.setString(2, plan.getDetail());
            ps.setInt(3, plan.getId());

            int i = ps.executeUpdate();
            ps.close();
            return i > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDB.release(conn);
        }
        return false;
    }

    /**
     * 根据编号删除该条计划
     *
     * @param id
     * @return
     */
    public boolean delete(int id) {
        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            String sql = "update plan set state='0' where id=? ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            int i = ps.executeUpdate();
            ps.close();
            return i > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            BaseDB.release(conn);
        }
    }

    public boolean changeState(int id, String state) {
        String sql = "update plan set state=? where id=?";
        Connection conn = null;
        try {
            conn = BaseDB.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, state);
            ps.setInt(2, id);
            int i = ps.executeUpdate();
            ps.close();
            return i > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDB.release(conn);
        }
        return false;
    }
}
